#include "CFDPostOperatorsProbe.h"

#include "CFDPostDataManager/CFDPostDataManager.h"

#include "GUIFrame/MainWindow.h"
#include "GUIFrame/MainTreeWidget.h"

#include "GUIWidget/GUIPickInfo.h"
#include "GUIWidget/PickedData.h"
#include "GUIWidget/PickedDataProvider.h"

#include "CFDPostGUIDialog/CFDPostProbeDialog.h"
#include "CFDPostGUIDialog/CFDPostGUIDialogManager.h"
#include "CFDPost/CFDPostOperatorsGUI/CFDPostOperModelTreeMouseEvent.h"
#include "CFDPost/CFDPostOperatorsInterface/CFDPostGraphEventOperator.h"

#include "FITK_Kernel/FITKAppFramework/FITKAppFramework.h"
#include "FITK_Kernel/FITKAppFramework/FITKGlobalData.h"
#include "FITK_Interface/FITKInterfaceCFDPost/FITKCFDPost3DManager.h"
#include "FITK_Interface/FITKInterfaceCFDPost/FITKCFDPostData.h"

#include <vtkUnstructuredGrid.h>

namespace OperModel
{
CFDPostOperatorsProbe::CFDPostOperatorsProbe()
{

}

bool CFDPostOperatorsProbe::execGUI()
{
    EventOper::CFDPostGraphEventOperator* graphOper = FITKOPERREPO->getOperatorT<EventOper::CFDPostGraphEventOperator>("OperGraphPreprocess");
    if(_senderName == "actionProbeCreate")
    {
        GUI::CFDPostProbeDialog* dialog = new GUI::CFDPostProbeDialog(
            DataManager::CFDPostDataManager::getInstance()->getTreeCurrentDataID(), this, true);
        dialog->show();
        if (graphOper == nullptr)return false;
        graphOper->reRender();
    }
    else if (_senderName == "actionProbeEdit")
    {
        int objID = -1;
        argValue(objID_String, objID);

        if (objID <= 0)
        {
            return false;
        }

        GUI::CFDPostProbeDialog* dialog = new GUI::CFDPostProbeDialog(objID, this, false);
        dialog->show();
        if (graphOper == nullptr)return false;
        graphOper->reRender();
    }

    return false;
}

bool CFDPostOperatorsProbe::execProfession()
{
    Interface::FITKCFDPost3DManager* manager = FITKAPP->getGlobalData()->getPostData<Interface::FITKCFDPostData>()->getPostDataManager();
    if (manager == nullptr)return false;
    EventOper::CFDPostGraphEventOperator* graphOper =FITKOPERREPO->getOperatorT<EventOper::CFDPostGraphEventOperator>("OperGraphPreprocess");
    if(graphOper==nullptr)return false;
    GUIOper::CFDPostOperModelTreeMouseEvent* treeOper =FITKOPERREPO->getOperatorT<GUIOper::CFDPostOperModelTreeMouseEvent>("CFDPostModelTreeEvent");
    if (treeOper == nullptr)return false;

    int objID = -1;
    argValue(objID_String, objID);
    Interface::FITKAbstractCFDPostData* data = manager->getDataByID(objID);
    if (data)
    {
        /*graphOper->updateSingle(objID);*/
    }

    /*treeOper->updateTree();*/
    return false;
}

void CFDPostOperatorsProbe::eventProcess(int index, QHash<int, QVariant> value)
{
    //点选择事件
    if (index == 0) {
        //拾取信息设置
        GUI::GUIPickInfoStru pinfo;
        pinfo._pickObjType = GUI::GUIPickInfo::PickObjType::POBJVert;
        pinfo._pickMethod = GUI::GUIPickInfo::PickMethod::PMSingle;
        //保存参数
        GUI::GUIPickInfo::SetPickInfo(pinfo);

        //拾取对象获取事件绑定
        GraphData::PickedDataProvider* pickD = GraphData::PickedDataProvider::getInstance();
        if (pickD == nullptr) return;
        connect(pickD, SIGNAL(sigPickDataOver()), this, SLOT(slotPickPointDataOver()));
    }
    //单元选取事件
    else if (index == 1) {
        //拾取信息设置
        GUI::GUIPickInfoStru pinfo;
        pinfo._pickObjType = GUI::GUIPickInfo::PickObjType::POBJEdge;
        pinfo._pickMethod = GUI::GUIPickInfo::PickMethod::PMSingle;
        //保存参数
        GUI::GUIPickInfo::SetPickInfo(pinfo);

        //拾取对象获取事件绑定
        GraphData::PickedDataProvider* pickD = GraphData::PickedDataProvider::getInstance();
        if (pickD == nullptr) return;
        connect(pickD, SIGNAL(sigPickDataOver()), this, SLOT(slotPickCellDataOver()));
    }
}

void CFDPostOperatorsProbe::slotPickPointDataOver()
{
    // //拾取对象获取事件绑定
    // GraphData::PickedDataProvider* pickD = GraphData::PickedDataProvider::getInstance();
    // if (pickD == nullptr) return;
    // disconnect(pickD, SIGNAL(sigPickDataOver()), this, SLOT(slotPickPointDataOver()));

    // //拾取信息设置
    // GUI::GUIPickInfoStru pinfo;
    // pinfo._pickObjType = GUI::GUIPickInfo::PickObjType::POBJNone;
    // pinfo._pickMethod = GUI::GUIPickInfo::PickMethod::PMNone;
    // //保存参数
    // GUI::GUIPickInfo::SetPickInfo(pinfo);

    // GraphData::PickedData* pickData = pickD->getPickDatas();
    // if (pickData == nullptr)return;
    // vtkUnstructuredGrid* grid = pickData->getGrid();
    // if (grid == nullptr)return;

    // //获取极值界面
    // GUI::CFDPostGUIDialogManager* dialogManager = GUI::CFDPostGUIDialogManager::getInstance();
    // QList<GUI::CFDPostProbeDialog*> probeDialogs = dialogManager->getDialog<GUI::CFDPostProbeDialog>();
    // for (GUI::CFDPostProbeDialog* pD : probeDialogs) {
    //     if (pD == nullptr)continue;
    //     for(int i = 0;i<pickData->getPointIDs().size();i++){
    //         if (grid->GetPoints() == nullptr)return;
    //         double* point = grid->GetPoints()->GetPoint(i);
    //         pD->insertPoint(point, pickData->getPointIDs()[i]);
    //     }
    // }

    // pickD->clearAll();
    // EventOper::CFDPostGraphEventOperator* graphOper =FITKOPERREPO->getOperatorT<EventOper::CFDPostGraphEventOperator>("OperGraphPreprocess");
    // if(graphOper==nullptr)return;
    // graphOper->reRender();
}

void CFDPostOperatorsProbe::slotPickCellDataOver()
{
    // //拾取对象获取事件绑定
    // GraphData::PickedDataProvider* pickD = GraphData::PickedDataProvider::getInstance();
    // if (pickD == nullptr) return;
    // disconnect(pickD, SIGNAL(sigPickDataOver()), this, SLOT(slotPickCellDataOver()));

    // //拾取信息设置
    // GUI::GUIPickInfoStru pinfo;
    // pinfo._pickObjType = GUI::GUIPickInfo::PickObjType::POBJNone;
    // pinfo._pickMethod = GUI::GUIPickInfo::PickMethod::PMNone;
    // //保存参数
    // GUI::GUIPickInfo::SetPickInfo(pinfo);

    // GraphData::PickedData* pickData = pickD->getPickDatas();
    // if (pickData == nullptr)return;
    // //获取极值界面
    // GUI::CFDPostGUIDialogManager* dialogManager = GUI::CFDPostGUIDialogManager::getInstance();
    // QList<GUI::CFDPostProbeDialog*> probeDialogs = dialogManager->getDialog<GUI::CFDPostProbeDialog>();
    // for (GUI::CFDPostProbeDialog* pD : probeDialogs) {
    //     if (pD == nullptr)continue;
    //     QList<int> cellIds = pickData->getCellIDs();
    //     for (int cellID : cellIds) {
    //         pD->insertCell(cellID);
    //     }
    // }
    // pickD->clearAll();
    // EventOper::CFDPostGraphEventOperator* graphOper =FITKOPERREPO->getOperatorT<EventOper::CFDPostGraphEventOperator>("OperGraphPreprocess");
    // if(graphOper==nullptr)return;
    // graphOper->reRender();
}
}
